package com.lmf.controller;

import com.lmf.domain.Student;
import com.lmf.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentServiceImpl;

    //注册学生
    @RequestMapping("/addStudent.do")
    public ModelAndView addStudent(Student student){
        ModelAndView mv=new ModelAndView();
        String tips="注册失败";
        int nums = studentServiceImpl.addStudent(student);
        if (nums > 0) {
            //注册成功
            tips="学生【"+student.getName()+"】注册成功";
        }
        //添加数据
        mv.addObject("tips", tips);
        //指定结果页面
        mv.setViewName("result");
        return mv;
    }

    //查询全部学生,响应ajax请求
    @RequestMapping("/selectStudent.do")
    @ResponseBody
    public List<Student> selectStudents(HttpServletResponse response, Student student){
        List<Student> students = studentServiceImpl.findStudents();
        return students;
    }
}
